import java.text.SimpleDateFormat;
import java.util.*;

public class Sales {
    private static Map<String, List<TicketInfo>> salesData = new HashMap<>();

    public static class TicketInfo {
        public String movieName;
        public String sessionTime;
        public String seatNumber;
        public double originalPrice;  // 票面原价
        public double actualPrice;    // 实际支付金额(儿童票为原价60%)
        public double refundAmount;   // 实际退款金额
        public boolean isChildTicket;
        public Date sellingTime;
        public boolean isRefunded;
    }

    // 添加售票记录
    public static void addSale(TicketInfo ticket) {
        String date = new SimpleDateFormat("yyyy-MM-dd").format(ticket.sellingTime);
        ticket.actualPrice = ticket.isChildTicket ? ticket.originalPrice * 0.6 : ticket.originalPrice;
        salesData.computeIfAbsent(date, k -> new ArrayList<>()).add(ticket);
    }

    // 添加退票记录
    public static void addRefund(TicketInfo refundTicket) {
        String date = new SimpleDateFormat("yyyy-MM-dd").format(refundTicket.sellingTime);
        List<TicketInfo> tickets = salesData.get(date);

        for (TicketInfo ticket : tickets) {
            if (ticket.seatNumber.equals(refundTicket.seatNumber) &&
                    ticket.sessionTime.equals(refundTicket.sessionTime)) {
                ticket.isRefunded = true;
                ticket.refundAmount = refundTicket.refundAmount;
                return;
            }
        }
    }

    // 按日期显示销售数据
    private void showSalesDataByDate(String date) {
        List<TicketInfo> tickets = salesData.getOrDefault(date, Collections.emptyList());
        Map<String, Map<String, SessionStats>> stats = new HashMap<>();

        // 初始化统计变量
        int adultSold = 0, childSold = 0;
        double adultRevenue = 0, childRevenue = 0;
        double refundTotal = 0;
        int adultRefunded = 0, childRefunded = 0;

        // 按电影和场次分组统计
        for (TicketInfo ticket : tickets) {
            // 先统计所有票的销售情况（无论是否退票）
            if (ticket.isChildTicket) {
                childSold++;
                childRevenue += ticket.actualPrice;
            } else {
                adultSold++;
                adultRevenue += ticket.originalPrice;
            }

            // 再单独统计退票情况
            if (ticket.isRefunded) {
                refundTotal += ticket.refundAmount;
                if (ticket.isChildTicket) {
                    childRefunded++;
                } else {
                    adultRefunded++;
                }
            }
        }

        // 打印统计结果
        System.out.println("\n=== " + date + " 销售数据 ===");
        System.out.printf("成人票: 售出%d张 | 单价%.2f | 总价%.2f\n",
                adultSold, adultSold > 0 ? adultRevenue/adultSold : 0, adultRevenue);
        System.out.printf("儿童票: 售出%d张 | 单价%.2f | 总价%.2f\n",
                childSold, childSold > 0 ? childRevenue/childSold : 0, childRevenue);
        System.out.printf("退票: 成人%d张/儿童%d张 | 退票总额%.2f\n",
                adultRefunded, childRefunded, refundTotal);
        System.out.printf("净销售额: %.2f\n", (adultRevenue + childRevenue) - refundTotal);
    }

    // 场次统计内部类
    private static class SessionStats {
        int adultSales = 0;
        int childSales = 0;
        double adultRevenue = 0;
        double childRevenue = 0;
        int adultRefunds = 0;
        int childRefunds = 0;
        double totalRefunds = 0;
    }

    // 时间范围统计方法
    public void showDailySales() {
        showSalesDataByDate(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
    }

    public void showWeeklySales() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        for (int i = 0; i < 7; i++) {
            showSalesDataByDate(sdf.format(cal.getTime()));
            cal.add(Calendar.DATE, 1);
        }
    }

    public void showMonthlySales() {
        Calendar cal = Calendar.getInstance();
        int maxDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        for (int i = 1; i <= maxDay; i++) {
            cal.set(Calendar.DAY_OF_MONTH, i);
            showSalesDataByDate(sdf.format(cal.getTime()));
        }
    }
}


